# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None


# 可以奇偶层逻辑分离，奇数层左到右，偶数层右到左
# 可以双端队列，奇数层尾进， 偶数层头进

class Solution:
    def levelOrder(self, root: TreeNode) -> List[List[int]]:
        res = []
        if root is not None:
            queue = [root]
            while queue:
                cur_res = []
                for i in range(len(queue)):
                    node = queue.pop(0)
                    cur_res.append(node.val)
                    if node.left is not None:
                        queue.append(node.left)
                    if node.right is not None:
                        queue.append(node.right)
                res.append(cur_res[::-1] if len(res)%2 else cur_res)
        return res